source('/home/jc148322/scripts/libraries/cool_functions.r')
library(SDMTools)
timeline='Post_decline' # or 'Pre_decline'

wd= paste('/home/jc148322/frog_declines/SDM2/',timeline,'/models/',sep='') #your /models/ directory
setwd(wd)
out.dir=paste('/home/jc148322/frog_declines/SDM2/',timeline,'/images/',sep='');dir.create(out.dir)

species=list.files(wd)

cols=colorRampPalette(c('tan','forestgreen','olivedrab2'))(100)
	
base.asc=read.asc.gz('/home/jc148322/frog_declines/SDM/Pre_decline/bioclim_12_15/models/acuti/output/ascii/current.76to05.asc.gz')		
for (spp in species) { cat(spp,'\n')
	spp.dir=paste(wd,spp,'/',sep='')
	var.dirs=list.files(spp.dir)
	var.dirs=grep('bioclim',var.dirs,value=TRUE)
	var.dirs=var.dirs[rev(order(sapply(strsplit(var.dirs,'\\.'),length)))] #reorder by decreasing model complexity
	w=round((length(var.dirs)+0.1)/2) # get the width of the image... 4.5 rounds to 4, so add 0.1 so that it will round to 5
	png(paste(out.dir,spp,'.test.png',sep=''),width=dim(base.asc)[1]*w, height=dim(base.asc)[2]*2, units='px', pointsize=20, bg='white')
	par(mfrow=c(2,w),mar=c(5,5,10,5))
	for(v in var.dirs){ cat(v,'\n')
		var.dir=paste(spp.dir,v,'/',sep='');setwd(var.dir)
	
		tasc=read.asc.gz('output/ascii/current.76to05.asc.gz')
		occur=read.csv('occur.csv',as.is=T)
		auc=read.csv('output/maxentResults.csv',as.is=TRUE)
		auc=auc[1,6]
		
		image(tasc,ann=F,axes=F,col=cols,zlim=c(0,1))
		points(occur$long,occur$lat,pch=16,cex=2)
		mtext(gsub('\\.0','\\.',v),line=6,side=3,cex=1.8)
		mtext(paste('AUC: ',auc,sep=''),line=1, side=3, cex=2.5)
		
	}
	dev.off()
}

